﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentCollectionService : BaseService, IEquipmentCollectionService
    {
        private readonly IEquipmentCollectionRepository _equipmentCollectionRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentCollectionService(IMapper mapper, IEquipmentCollectionRepository _equipmentCollectionRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._equipmentCollectionRepository = _equipmentCollectionRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentCollection(EquipmentCollection equipmentCollection)
        {
            _equipmentCollectionRepository.AddEntity(equipmentCollection);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentCollection(long id)
        {
            var cust = _equipmentCollectionRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _equipmentCollectionRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<EquipmentCollection> GetEquipmentCollection(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _equipmentCollectionRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _equipmentCollectionRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentCollection? GetEquipmentCollectionById(long id)
        {
            return _equipmentCollectionRepository.QueryEntityById(id);
        }
        
        public List<EquipmentCollection> GetEquipmentReceiveMsgDown()
        {
            var list = _equipmentCollectionRepository.GetAllEntityList();
            return list;
        }

        public bool UpdEquipmentCollection(EquipmentCollection EquipmentCollection)
        {
            var aobj = _equipmentCollectionRepository.QueryIsEntityWhere(x => x.Id == EquipmentCollection.Id);
            if (aobj == null)
            {
                return false;
            }
            _equipmentCollectionRepository.UpdEntity(EquipmentCollection);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
